﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace AtomPuzzleSolver.Model
{
    public class Molecule
    {
        public IList<Atom> Atoms { get; set; }
        public Color Color { get; set; }

        public Molecule()
        {
            Atoms = new List<Atom>();
            Atoms.Add(new Atom(this,0,0));
            Color = Color.Black;
        }

        public Molecule(Color color)
        {
            Atoms = new List<Atom>();
            Atoms.Add(new Atom(this, 0, 0));
            Color = color;
        }

        public Atom AddAtom(int index, Direction direction)
        {
            Bonding newBonding = Atoms[index].Bonding.GetDirection(direction);

            if (!(Atoms.Count(a => a.Bonding == newBonding) == 0))
            {
                throw new InvalidOperationException("Another Atom is already at this location");
            }
            else
            {
                var atom = new Atom(this, newBonding);
                Atoms.Add(atom);

                return atom;
            }
        }

        public void Rotate()
        {
            foreach (var atom in Atoms)
            {
                atom.Bonding = atom.Bonding.Rotate();
            }
        }

        public void Mirror()
        {
            foreach (var atom in Atoms)
            {
                atom.Bonding = atom.Bonding.Mirror();
            }
        }
    }
}
